using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ListaFacil
{
    public partial class frmCadastroOrelhoes : Form
    {
        private DataTable dtCidade;
        private DataTable dtLogradouro;
        private DataTable dtOrelhoes;
        private BindingSource bsCidade = new BindingSource();
        private BindingSource bsLogradouro = new BindingSource();
        private BindingSource bsOrelhoes = new BindingSource();
        private Boolean editMode = false;

        public frmCadastroOrelhoes()
        {
            InitializeComponent();
        }
        private void atualizarGrid() 
        {
            dtCidade = Program.SLF.consultarCidadesCadastradas();
            dtLogradouro = Program.SLF.ConsultaLogradouro();
            dtOrelhoes = Program.SLF.ConsultaOrelhoesCadastrados();
            bsCidade.DataSource = this.dtCidade;
            bsLogradouro.DataSource = this.bsLogradouro;
            this.bindingNavigator1.BindingSource = this.bsOrelhoes;
            this.dataGridView1.DataSource = this.bsOrelhoes;
        }

        private void frmCadastroOrelhoes_Load(object sender, EventArgs e)
        {
            dtCidade = Program.SLF.consultarCidadesCadastradas();
            dtLogradouro = Program.SLF.ConsultaLogradouro();
            dtOrelhoes = Program.SLF.ConsultaOrelhoesCadastrados();
            bsCidade.DataSource = this.dtCidade;
            bsLogradouro.DataSource = this.dtLogradouro;
            this.bindingNavigator1.BindingSource = this.bsOrelhoes;
            this.dataGridView1.DataSource = this.bsOrelhoes;
            cboCidade.DataSource = dtCidade;
            cboCidade.DisplayMember = "Cidade";
            cboCidade.ValueMember="ID";
            cboLogradouro.DisplayMember = "EnderecoCompleto";
            cboLogradouro.ValueMember="ID";
            this.HabilitaBotoes(true);
        }

        private void HabilitaBotoes(bool flagHabilita)
        {
            bindingNavigatorAddNewItem.Enabled = flagHabilita;
            bindingNavigatorDeleteItem.Enabled = flagHabilita;
            bindingNavigatorMoveFirstItem.Enabled = flagHabilita;
            bindingNavigatorMoveLastItem.Enabled = flagHabilita;
            bindingNavigatorMoveNextItem.Enabled = flagHabilita;
            bindingNavigatorMovePreviousItem.Enabled = flagHabilita;
            bindingNavigatorPositionItem.Enabled = flagHabilita;
            editToolStripButton.Enabled = flagHabilita;
            saveToolStripButton.Enabled = !flagHabilita;
            cancelToolStripButton.Enabled = !flagHabilita;
            txtNumero.Enabled = !flagHabilita;
            txtNumeroEndereco.Enabled = !flagHabilita;
            dataGridView1.Enabled = flagHabilita;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null)
            {
               cboCidade.SelectedText=dataGridView1.CurrentRow.Cells["Cidade"].Value.ToString();
               cboLogradouro.SelectedText=dataGridView1.CurrentRow.Cells["Logradouro"].Value.ToString();
               txtNumeroEndereco.Text = dataGridView1.CurrentRow.Cells["Numero_Endereco"].Value.ToString();
               txtNumero.Text = dataGridView1.CurrentRow.Cells["Numero"].Value.ToString();
               this.Navegacao(this, e);
            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
        }

        private void EdittoolStripButton_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
            this.editMode = true;
        }

        private void canceltoolStripButton_Click(object sender, EventArgs e)
        {
            this.atualizarGrid();
            this.HabilitaBotoes(true);
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            int numeroregistro = 0;
            int linha = 0;
            string retorno = "";

            linha = Convert.ToInt32(bindingNavigatorPositionItem.Text) - 1;
            numeroregistro = Convert.ToInt32(dataGridView1.Rows[linha].Cells[0].Value);

            retorno = Program.SLF.removerRegiao(numeroregistro);

            if (retorno != "")
                MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);

            this.atualizarGrid();
            this.HabilitaBotoes(true);
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            string retorno = "";
            int numeroregistro = 0;
            int linha = 0;

            if (this.editMode == false)
            {
                retorno = Program.SLF.inserirOrelhao(Int32.Parse( txtNumero.Text), txtNumeroEndereco.Text,Int32.Parse( cboCidade.SelectedText),Int32.Parse( cboLogradouro.SelectedText));

                if (retorno != "")
                    MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                linha = Convert.ToInt32(bindingNavigatorPositionItem.Text) - 1;
                numeroregistro = Convert.ToInt32(dataGridView1.Rows[linha].Cells[0].Value);
                //retorno = Program.SLF.updateOrelhao(txtId.text,Int32.Parse( cboCidade.SelectedValue), Int32.Parse( cboLogradouro.SelectedText), txtNumeroEndereco.Text,Int32.Parse( txtNumero.Text));

                if (retorno != "")
                    MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.atualizarGrid();
            this.HabilitaBotoes(true);
        }

        private void txtNumero_TextChanged(object sender, EventArgs e)
        {
            if (this.editMode)
                this.HabilitaBotoes(false);
        }

        private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) == false)
                e.Handled = true;
        }

        private void Navegacao(object sender, EventArgs e)
        {
            this.editMode = false;
        }

        private void Edicao(object sender, EventArgs e)
        {
            this.editMode = true;
        }

        private void cboCidade_SelectedValueChanged(object sender, EventArgs e)
        {
           


        }


      }
}